package com.task.manager.config.quartz;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.task.manager.domain.TaskResult;
import com.task.manager.mapper.TaskResultMapper;

@RestController("/task")
public class CallBackApi {
	
	private static Logger log = LoggerFactory.getLogger(CallBackApi.class);
	
	@Autowired
	private TaskResultMapper taskResultMapper;
	
	@RequestMapping("/callBack")
	@ResponseBody
	public Boolean callBack(@RequestParam("result")Boolean result,@RequestParam("remark")String remark,@RequestParam("resultId")String resultId) {
		if(result == null || StringUtils.isEmpty(resultId)) {
			return false;
		}
		
		Integer id = null;
		try {
			id = Integer.parseInt(resultId);
		} catch (Exception e) {
			log.error("回调接口参数错误："+e.getMessage(),e);
			return false;
		}
		
		TaskResult taskResult = taskResultMapper.selectByPrimaryKey(id);
		if(taskResult.getResult()==null) {
			taskResult.setResult(result);
			taskResult.setRemark(remark);
			taskResultMapper.updateByPrimaryKey(taskResult);
			return true;
		}else {
			return false;
		}
	}
}
